package cn.smile.controller.home;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smile.bean.PageBean;
import cn.smile.bean.Result;
import cn.smile.entity.common.Goods;
import cn.smile.entity.common.GoodsCategory;
import cn.smile.service.common.CommentService;
import cn.smile.service.common.GoodsCategoryService;
import cn.smile.service.common.GoodsService;

import javax.annotation.Resource;

/**
 * 前台物品管理控制器
 */
@RequestMapping("/home/goods")
@Controller
public class HomeGoodsController {

    @Resource
    private GoodsCategoryService goodsCategoryService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private CommentService commentService;

    /**
     * 物品详情页面
     */
    @RequestMapping(value = "/detail")
    public String detail(@RequestParam(name = "id") Long id, Model model) {
        Goods goods = goodsService.findById(id);
        if (goods == null) {
            model.addAttribute("msg", "物品不存在！");
            return "error/runtime_error";
        }
        model.addAttribute("goods", goods);
        model.addAttribute("commentList", commentService.findByGoods(goods));
        //更新商品浏览量
        goods.setViewNumber(goods.getViewNumber() + 1);
        goodsService.save(goods);
        return "home/goods/detail";
    }

    /**
     * 根据商品分类搜索商品信息
     */
    @RequestMapping(value = "/list")
    public String list(@RequestParam(name = "cid") Long cid, PageBean<Goods> pageBean, Model model) {
        GoodsCategory goodsCategory = goodsCategoryService.findById(cid);
        if (goodsCategory == null) {
            model.addAttribute("msg", "物品分类不存在！");
            return "error/runtime_error";
        }
        //接下来分两步来考虑，分类是否是二级分类。
        List<Long> ids = new ArrayList<>();
        ids.add(goodsCategory.getId());
        if (goodsCategory.getParent() == null) {
            //选中的是顶级分类，此时需要获取该顶级分类下的所有子分类
            List<GoodsCategory> findChildren = goodsCategoryService.findChildren(goodsCategory);
            for (GoodsCategory gc : findChildren) {
                ids.add(gc.getId());
            }
        }
        model.addAttribute("pageBean", goodsService.findlist(ids, pageBean));
        model.addAttribute("gc", goodsCategory);
        return "home/goods/list";
    }

    /**
     * 获取已出售的商品总数
     */
    @RequestMapping(value = "/get_sold_total", method = RequestMethod.POST)
    @ResponseBody
    public Result<Long> getSoldTotal() {
        return Result.success(goodsService.getSoldTotalCount());
    }
}
